package com.os.crm.controller;

import com.os.crm.pojo.Enterprise;
import com.os.crm.service.EnterpriseService;
import com.os.crm.service.POIService;
import com.os.sys.service.bean.UserInfo;
import com.os.util.Tools;
import com.os.util.page.PageRequest;
import com.os.util.page.PageResult;
import entity.Param;
import entity.Result;
import entity.StatusCode;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/enterprise")
@RefreshScope
@Slf4j
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @ApiOperation(value = "分页查询企业信息列表信息" , notes = "分页查询企业信息列表信息")
    @RequestMapping(value="/selectByPage", method=RequestMethod.GET)
    public Result selectByPage(@RequestParam(value="page",required = false) Integer page,
                               @RequestParam(value="pageSize",required = false) Integer pageSize
            ,@RequestParam(value="ent_name",required = false) String ent_name
            ,@RequestParam(value="phone",required = false) String phone
            ,@RequestParam(value="address",required = false) String address
            ,@RequestParam(value="create_time",required = false) String create_time
            ,@RequestParam(value="business_code",required = false) String business_code
            ,@RequestParam(value="business_name",required = false) String business_name
            ,@RequestParam(value="region_code",required = false) String region_code
            ,@RequestParam(value="region_name",required = false) String region_name
    ){
        Map map = new HashMap();
        List paramList = new ArrayList();
        if(ent_name!=null){
            paramList.add(new Param("ent_name", ent_name + "", "String","like"));
        }
        if(phone!=null){
            paramList.add(new Param("phone", phone + "", "String","like"));
        }
        if(address!=null){
            paramList.add(new Param("address", address + "", "String","like"));
        }
        if(create_time!=null){
            paramList.add(new Param("create_time", create_time + "", "String","like"));
        }
        if(business_code!=null){
            paramList.add(new Param("business_code", business_code + "", "String","like"));
        }
        if(business_name!=null){
            paramList.add(new Param("business_name", business_name + "", "String","like"));
        }
        if(region_code!=null){
            paramList.add(new Param("region_code", region_code + "", "String","like"));
        }
        if(region_name!=null){
            paramList.add(new Param("region_name", region_name + "", "String","like"));
        }
        map.put("condstr", Tools.genCondStr("", paramList));
        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(page);
        pageRequest.setPageSize(pageSize);
        PageResult pageResult = enterpriseService.selectByPage(pageRequest,map);
        Result result = new Result(true, StatusCode.OK,"查询成功",pageResult.getContent());
        result.setTotalCount(pageResult.getTotalSize());
        return result;
    }
    @ApiOperation(value = "按照ID查询企业信息信息" , notes = "按照ID查询企业信息信息")
    @ApiImplicitParam(name = "ent_id",value = "ID",required = true,dataType = "int")
    @RequestMapping(value="/selectById/{ent_id}", method=RequestMethod.GET)
    public Result selectById(@PathVariable int ent_id){
        return new Result(true, StatusCode.OK,"查询成功", enterpriseService.selectById(ent_id));
    }
    @ApiOperation(value = "新增企业信息信息" , notes = "新增企业信息信息")
    @RequestMapping(value="/add",method = RequestMethod.POST)
    public Result insert(@RequestBody Enterprise enterprise){
        enterpriseService.insert(enterprise);
        return new Result(true, StatusCode.OK,"添加成功");
    }
    @ApiOperation(value = "更新企业信息信息" , notes = "更新企业信息信息")
    @RequestMapping(value="/update",method = RequestMethod.POST)
    public Result update(@RequestBody Enterprise enterprise){
        enterpriseService.update(enterprise);
        return new Result(true, StatusCode.OK,"修改成功", enterprise);
    }
    @ApiOperation(value = "删除企业信息信息" , notes = "删除企业信息信息")
    @RequestMapping(value="/{ent_id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable int ent_id){
        enterpriseService.deleteById(ent_id);
        return new Result(true, StatusCode.OK,"删除成功");
    }

}
